from datetime import datetime
from typing import Literal, Optional

from pydantic import Field, field_serializer, field_validator

from src.models.base import IdenticalModel
from src.utils.date_utils import parse_date


class ShuoshiRecord(IdenticalModel):
    id: str = Field(
        title="学号",
        min_length=1,
        description="学号，不允许为空",
        serialization_alias="学号",
    )
    name: str = Field(
        title="姓名",
        min_length=1,
        description="姓名，不允许为空",
        serialization_alias="姓名",
    )
    education_level: Literal["硕士"] = Field(
        title="培养层次",
        default="硕士",
        description="培养层次，只能为“硕士”",
        serialization_alias="培养层次",
    )
    learning_mode: Literal["全日制", "非全日制"] = Field(
        title="学习方式",
        default="全日制",
        description="学习方式，只能为“全日制”或“非全日制”",
        serialization_alias="学习方式",
    )
    college: str = Field(title="学院", serialization_alias="学院")

    student_category: str = Field(title="学生类别", serialization_alias="学生类别")
    education_mode: str = Field(title="培养模式", serialization_alias="培养模式")

    is_hac_student: Literal["是", "否"] = Field(
        title="是否港澳台学生",
        default="否",
        description="是否港澳台学生，只能为“是”或“否”",
        serialization_alias="是否港澳台学生",
    )
    phone_number: str = Field(title="联系电话", serialization_alias="联系电话")

    political_status: str = Field(title="政治面貌", serialization_alias="政治面貌")
    id_card_number: str = Field(title="证件号码", serialization_alias="证件号码")

    archive_arrvial_status: Literal["已到档", "未到档", ""] = Field(
        title="到档情况",
        default="未到档",
        description="到档情况，只能为“已到档”、“未到档”或留空",
        serialization_alias="到档情况",
    )
    archive_arrvial_time: Optional[datetime] = Field(
        title="到档时间",
        default=None,
        description="到档时间，格式为YYYY-MM-DD，可以留空",
        serialization_alias="到档时间",
    )
    party_archive_lend_record: str = Field(
        title="党员档案借出情况记录",
        default="",
        serialization_alias="党员档案借出情况记录",
    )
    degree_archive_status: Literal["已归档", "未归档", ""] = Field(
        title="学位档案归档状态",
        default="",
        description="学位档案归档状态，只能为“已归档”、“未归档”或留空",
        serialization_alias="档案归档情况",
    )
    graduate_registration_form_status: Literal["已归档", "未归档", ""] = Field(
        title="毕业登记表状态",
        default="",
        description="毕业登记表状态，只能为“已归档”、“未归档”或留空",
        serialization_alias="毕业登记表状态",
    )
    graduation_status: Literal["毕业", "在读", "肄业", "结业", "退学", ""] = Field(
        title="毕业状态",
        default="在读",
        description="毕业状态，只能为“毕业”、“在读”、“肄业”、“结业”、“退学”或留空",
        serialization_alias="毕业状态",
    )
    archive_delivery_status: Literal["已寄出", "未寄出", ""] = Field(
        title="档案寄出情况",
        default="未寄出",
        description="档案寄出情况，只能为“已寄出”、“未寄出”或留空",
        serialization_alias="档案寄出情况",
    )

    @field_serializer("archive_arrvial_time")
    def format_archive_arrvial_time(self, v: datetime):
        return v.strftime("%Y-%m-%d") if v is not None else None

    @field_validator("archive_arrvial_time", mode="before")
    def parse_archive_arrvial_time(cls, v):
        if isinstance(v, str):
            return parse_date(v)
        return v